import Mock from 'mockjs';
import UserData from "./data/UserData";

Mock.mock(/\/login/, 'post', (options) => {
  const { body } = options;
  const { username, password } = JSON.parse(body);

  // Read user data from localStorage or default data
  const userList = getLocalUserList();

  // Find the user with the provided username and password
  const user = userList.find(user => user.username === username && password === '123456');

  // If user is found, return login successful response with user's username and role
  if (user) {
    return { code: 1, msg: 'Login successful', result: { username: user.username, role: user.role } };
  } else {
    return { code: 0, msg: 'Incorrect username or password', result: null };
  }
});

// 获取本地存储中的用户列表
const getLocalUserList = () => {
  const localUserList = localStorage.getItem('userList');
  // 要么读取localStorage，要不读取我默认得用户数据
  return localUserList ? JSON.parse(localUserList) : UserData;
};

// 保存用户列表到本地存储
const setLocalUserList = (userList) => {
  localStorage.setItem('userList', JSON.stringify(userList));
};

// 获取用户列表
Mock.mock(/\/user\/list/, 'get', () => {
  const userList = getLocalUserList(); // 每次获取列表时从本地存储读取最新数据
  return { code: 1, msg: '获取成功', result: userList };
});

// 添加用户
Mock.mock(/\/user\/add/, 'post', (options) => {
  const { body } = options;
  const newUser = JSON.parse(body);
  const userList = getLocalUserList(); // 每次获取列表时从本地存储读取最新数据
  userList.push(newUser);
  setLocalUserList(userList); // 添加后保存到本地存储
  return { code: 1, msg: '添加成功', result: userList };
});

// 编辑用户
Mock.mock(/\/user\/edit/, 'post', (options) => {
  const { body } = options;
  const userList = getLocalUserList(); // 每次获取列表时从本地存储读取最新数据
  const editedUser = JSON.parse(body);
  const index = userList.findIndex((ele) => ele.key === editedUser.key);
  if (index !== -1) {
    userList.splice(index, 1, editedUser);
    setLocalUserList(userList); // 编辑后保存到本地存储
  }
  return { code: 1, msg: '修改成功', result: userList };
});

// 模拟修改用户角色的接口
Mock.mock(/\/user\/roleEdit/, 'post', (options) => {
  const { body } = options;
  const { key, newRole } = JSON.parse(body);
  const userList = getLocalUserList(); // 每次获取列表时从本地存储读取最新数据
  // 在 UserData 中查找对应的用户
  const index = userList.findIndex(user => user.key === key);

  // 如果找到了对应的用户，则更新用户的角色
  if (index !== -1) {
    userList[index].role = newRole;
    setLocalUserList(userList); // 编辑后保存到本地存储
  }

  return { code: 1, msg: '修改用户角色成功', result: UserData };
});

// 获取单个用户
Mock.mock(RegExp(`/user/get.*`), 'get', (options) => {
  const id = options.url.match(/\/user\/get\/(\w+)/)[1];
  const userList = getLocalUserList(); // 每次获取列表时从本地存储读取最新数据
  const data = userList.find((ele) => ele.key === parseInt(id));
  return { code: 1, msg: '获取成功', result: data };
});

// 删除用户
Mock.mock(RegExp(`/user/delete.*`), 'delete', (options) => {
  const id = options.url.match(/\/user\/delete\/(\w+)/)[1];
  const userList = getLocalUserList(); // 每次获取列表时从本地存储读取最新数据
  const index = userList.findIndex((ele) => ele.key === parseInt(id));
  if (index !== -1) {
    userList.splice(index, 1);
    setLocalUserList(userList); // 删除后保存到本地存储
  }
  return { code: 1, msg: '删除成功' };
});
